package leetcode.code1962;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int minStoneSum(int[] piles, int k) {
		int sum = 0, max = Integer.MIN_VALUE;
		int[] hash = new int[10001];
		for (int i = 0; i < piles.length; i++) {
			int num = piles[i];
			max = Math.max(max, num);
			sum += num;
			hash[num] += 1;
		}
		for (int num = max; num >= 0 && k > 0; num--) {
			int size = hash[num];
			if (size != 0) {
				int removeNum = num >> 1;
				int removeSize = size >= k ? k : size;
				sum -= removeNum * removeSize;
				k -= removeSize;
				hash[num - removeNum] += removeSize;
			}
		}
		return sum;
	}

	@Override
	public void debug4() {
		H.compare(12, this.minStoneSum(H.str2array3("[5,4,9]"), 2));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
